<?php

class Sistema_CoresController extends App_Controller_Action
{
    private $_table     = 'CORES';
    private $_pk        = 'COR_ID';
    private $_order_col = 'COR_ORD';
    private $_order     = 'ASC';
    private $_sta_col   = 'COR_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'COR_FILE'; 
    public  $dir        = 'public/upload/cores/';
    
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Cores();        
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $where = $this->_pk.' > 0';
        
        $lin_id = trim($this->_getParam('lin_id',0));
        
        if($lin_id == 0) $this->_helper->redirector('index','linha',$this->modulo,array());
        
        $where .= ' AND c.LIN_ID = '.$lin_id;
        
        $dscfiltro = trim($this->_getParam('dscfiltro', ''));
        
        if($dscfiltro != '') $where .= ' AND COR_NOM like "%'.$dscfiltro.'%"';
        
        $this->view->dscfiltro = $dscfiltro;
        
        $query = 'SELECT SQL_CACHE c.*, l.LIN_NOM FROM '.$this->_table.' c, LINHA l '
                .' WHERE c.LIN_ID = l.LIN_ID AND '.$where
                .' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
       
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js');
       
        Zend_Loader::loadClass('imagem','library');
        
        $lin_id = $this->_getParam('lin_id', 0);
        
        if($lin_id == 0) $this->_helper->redirector('index','linha',$this->modulo,array());
        
        $url = $this->_urlbase.'/'.$this->_modulo.'/'.$this->_controller.'/index/lin_id/'.$lin_id;
        
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    
                    $data = $this->form->getValues();
                    
                    unset($data['Salvar']);
                    unset($data[$this->_file_col]);
                    unset($data['img']);
                    
                    $this->db->beginTransaction();
                    if($id == 0){
                        $data['LIN_ID'] = $lin_id;
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();   
                        
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else {
                        $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    }
                    
                    // redimencionar
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp; // 1920 x 1080
                        
                        imagem::reizeFull($filename, $filename, '36', '36');
                    }
                    
                    
                    $this->db->commit();
                    
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','lin_id'=>$lin_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                    
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
                $this->db->rollBack();
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        $id = $this->_getParam('id', 0);
        if((int) $id > 0 ) {   
            $this->db->beginTransaction();
            try {
                $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
                
                Zend_Loader::loadClass('imagem','library');
                imagem::delete($id,$this->dir,TRUE);
            
                $this->db->commit();
            } catch(Exception $ex) {
                //echo getMessage();
                $ok = FALSE;
                $msg = MSG_DELETE;
                $this->db->rollBack();
            }             
        }       
        echo json_encode(array('ok' => $ok, 'msg'=> $msg));
        //$this->_helper->redirector('index');
    }
    
}